import { MessageData, TexRes } from '@/types/interface'
import { createSlice, PayloadAction } from '@reduxjs/toolkit'


// Define a type for the slice state
interface QueryItem {
    text: string
    file_name: string
    isSend: boolean
    conversationId: string
    loading: boolean
}
interface InitData {
    dirty: boolean;
    data: string[];
    think: string[];
    node: any;
    mode: string;
}
interface InitMap {
    [key: string]: InitData
}
interface InitState {
    historyData: MessageData,
    list: TexRes[],
    clear: boolean
}

// Define the initial state using that type
const initialState: InitState = {
    historyData: {},
    list: [],
    clear: false
}

export const counterSlice = createSlice({
    name: 'query',
    // `createSlice` will infer the state type from the `initialState` argument
    initialState,
    reducers: {
        // Use the PayloadAction type to declare the contents of `action.payload`
        addhistoryData: (state, action: PayloadAction<MessageData>) => {
            state.historyData = action.payload
        },
        addListData: (state, action: PayloadAction<TexRes[]>) => {
            // console.log(action.payload)
            state.list = action.payload
        },
        setClear:(state,action:PayloadAction<boolean>) =>{
            state.clear = action.payload
        }
    },
})

export const { addhistoryData, addListData,setClear } = counterSlice.actions

// Other code such as selectors can use the imported `RootState` type
export const selecthistoryData = (state: { query: InitState }): MessageData => state.query.historyData
export const selectList = (state: { query: InitState }): TexRes[] => state.query.list
export const selectClear = (state: { query: InitState }): boolean => state.query.clear

export default counterSlice.reducer